package com.fangjinwang.zhushunqing.Widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.os.Build;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.fangjinwang.zhushunqing.R;
import com.jaydenxiao.common.commonutils.AndroidUtil;

/**
 * Created by fjw_zhushunqing on 2017/1/4.
 */

public  class LoadingButton extends FrameLayout{
    private String text;
    private int text_color;
    private  ProgressBar progressBar;
    private  TextView textView;

    public LoadingButton(Context context) {
        super(context);
    }

    public LoadingButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context,attrs);
    }

    public void init( Context context,AttributeSet attrs){
        TypedArray array=getContext().obtainStyledAttributes(attrs, R.styleable.LoadingButton);
        text=array.getString(R.styleable.LoadingButton_texts);
        text_color=array.getColor(R.styleable.LoadingButton_text_colors,R.color.text_gray);
         textView=new TextView(context);
        LayoutParams params=new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        params.gravity=Gravity.CENTER;
//        textView.setPadding(10,10,10,10);
        textView.setText(text);
        textView.setTextColor(text_color);
        textView.setLayoutParams(params);
        this.addView(textView);

         progressBar = new ProgressBar(context);
        if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.LOLLIPOP) {
            progressBar.setProgressDrawable(context.getResources().getDrawable(com.dyhdyh.widget.loadingbar.R.drawable.loadingbar_progressbar_vertical));
        }
       int height=AndroidUtil.getViewSize(textView,2);
        FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(height, height);
        //lp.addRule(RelativeLayout.CENTER_IN_PARENT);
        lp.gravity = Gravity.CENTER;
        progressBar.setLayoutParams(lp);

        this.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if(progressBar.getParent()==null){
                    LoadingButton.this.addView(progressBar);
                    textView.setVisibility(INVISIBLE);
                }
                onCallBack.onClick(v);
            }
        });

    }

    public void setLoadingOnClickListener(LoadingOnClickListener onCallBac){
        this.onCallBack=onCallBac;
    }




    public LoadingOnClickListener onCallBack;
    public interface LoadingOnClickListener  {
        public void onClick(View v) ;
    }

    public void complete(String mtext){
        if(progressBar.getParent()==this){
            this.removeView(progressBar);
            textView.setVisibility(VISIBLE);
            textView.setText(text==null?text:mtext);
        }
    }

//    public View createDefaultLoadingView(Context mContext) {
////        FrameLayout relativeLayout = new FrameLayout(mContext);
////        relativeLayout.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
//        ProgressBar progressBar = new ProgressBar(mContext);
//        if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.LOLLIPOP) {
//            progressBar.setProgressDrawable(mContext.getResources().getDrawable(com.dyhdyh.widget.loadingbar.R.drawable.loadingbar_progressbar_vertical));
//        }
//        FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
//        //lp.addRule(RelativeLayout.CENTER_IN_PARENT);
//        lp.gravity = Gravity.CENTER;
//        progressBar.setLayoutParams(lp);
//        relativeLayout.addView(progressBar);
//        int color = relativeLayout.getResources().getColor(com.dyhdyh.widget.loadingbar.R.color.bg_loading);
//        relativeLayout.setBackgroundColor(color);
//        return relativeLayout;
//    }
}
